home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
036a
/
pmfinder.zip
/
MISC.C
< prev
next >
Wrap
Text File
|
1991-12-18
|
4KB
|
166 lines
#define INCL_PM
#define NUL '\0'
#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "misc.h"
extern BOOL pascal fContinue;
/*
The Match functions match a pattern to a filename.
These functions have changed slightly from
Don A. Williams orginal code.
I've removed references to global variables.
*
*/
int
Match (char *Str, char *Pat) {
char S_Name[66], S_Ext[4];
char P_Name[66], P_Ext[4];
char *p1;
if ( (p1 = strrchr(Str, '.')) != NULL ) {
*p1 = '\0';
strcpy(S_Name, Str);
strcpy(S_Ext, p1+1);
*p1 = '.';
}
else {
strcpy(S_Name, Str);
S_Ext[0] = '\0';
}
if ( (p1 = strchr(Pat, '.')) != NULL ) {
*p1 = '\0';
strcpy(P_Name, Pat);
strcpy(P_Ext, p1+1);
*p1 = '.';
}
else {
strcpy(P_Name, Pat);
strcpy(P_Ext, "*");
}
if ( !I_Match(S_Name, P_Name) ) return(0);
if ( (P_Ext[0] == '\0') && (S_Ext[0] != '\0') ) return(0);
if ( !I_Match(S_Ext, P_Ext) ) return(0);
return(1);
}
static int
I_Match (char *Str, char *Pat) {
char *p, *p1, *p2;
int t;
if ( (p1 = strchr(Pat, '*')) == NULL)
return( S_Match(Str, Pat, 1) );
if (Pat[0] != '*') {
*p1 = '\0';
t = S_Match(Str, Pat, 0);
*p1 = '*';
if (!t) return(0);
}
if (Pat[strlen(Pat)-1] != '*') {
p2 = strrchr(Pat, '*') + 1;
if ( !S_Match(&Str[strlen(Str) - strlen(p2)], p2, 1) )
return(0);
}
p = Str;
while ( (p2 = strchr(++p1, '*')) != NULL ) {
*p2 = '\0';
while ( (p = strchr(p, p1[0])) != NULL ) {
if ( S_Match(p, p1, 0) ) break;
++p1;
}
if (p == NULL) return(0);
p += strlen(p1);
*p2 = '*';
p1 = p2;
}
return(1);
}
static int
S_Match (char *S, char *P, int Anchor) {
while (*P != '\0') {
if ( (*S == *P) || (*P == '?') ) {
S++;
P++;
}
else return(0);
}
if ( Anchor && (*S != '\0') ) return(0);
return(1);
}
/*
* AddToListBox( hwndListBox, szEntry ) : VOID;
*
* hwndListBox handle to output listbox
* szEntry character sting to insert
*
* This function adds the string to the listbox and if not enough
* memory, will issue an error message box.
*
*/
void AddToListBox(HWND hwndListBox,char *szEntry)
{
SHORT sErr; /* error code */
if (fContinue)
{
sErr = SHORT1FROMMR( WinSendMsg(
hwndListBox,
LM_INSERTITEM,
MPFROMSHORT( LIT_END ),
szEntry));
switch ( sErr )
{
case LIT_MEMERROR:
WinMessageBox(
HWND_DESKTOP,
WinQueryWindow( hwndListBox, QW_PARENT, FALSE ),
"Too many files! Narrow the search criteria!",
"File Finder",
0,
MB_OK | MB_ICONHAND
);
fContinue = FALSE;
break;
case LIT_ERROR:
WinMessageBox(
HWND_DESKTOP,
WinQueryWindow( hwndListBox, QW_PARENT, FALSE ),
"Unable to Add Item to List Box!",
"File Finder",
0,
MB_OK | MB_ICONHAND
);
fContinue = FALSE;
break;
default:
break;
}
}
}
void *zalloc(size_t bytes)
{
void *ret = malloc(bytes);
if(!ret){
printf("Memory request for %u bytes failed\n", bytes);
exit(3);
}
return ret;
}